import axios from 'axios';
//let withPrefix = 'http://127.0.0.1:9999/api';  路径前缀是这样写的话代理不会生效
let withPrefix = '/api';

//先进行http为200的请求体里面errcode为401未授权的处理
let preHandleRes = function (res) {
    if (res.data && res.data.errcode != 0) {
        console.log('ajax异常,', res);
        // try {
        //这里后端定义可能为7080401，1002333333401，不管整样，模1000就是取最后三位，模10就是取最后一位
        if (res.data.errcode % 1000 == 401) {
           // window.location.href = "{tgac_rio_path}/index.html#/login";
            window.location.href = "/";
            setTimeout(function(){window.location.reload()},500);
        }else {
            console.log(res.data.errmsg);
            Message({
                message: res.data.errmsg,
                type: 'error'
            });
            return Promise.reject(res.data.errmsg);
        }
        // } catch (e) {
        //
        // }
    }
    return res.data;
}

let base = '/api';

export const postKeyValueRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let i in data) {
                ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + '&'
            }
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}
export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params
    })
}
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${base}${url}`,
        data: params
    })
}
export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: `${base}${url}`,
        params: params
    })
}
export const deleteRequest = (url, params) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`,
        params: params
    })
}





export const requestLogout = params => {
    return axios.get(`${withPrefix}/logout`, params);
};

export const requestLogin = params => {
    return axios.post(`${withPrefix}/login`, params).then(res => preHandleRes(res));
};

export const addModel = params => {
    return axios.post(`${withPrefix}/model/add`, params).then(res => preHandleRes(res));
};

export const getAppList = function () {
    return axios.post(`${withPrefix}/model/list`).then(res => preHandleRes(res));
};

export const getLeaveList = function () {
    return axios.post(`${withPrefix}/leave/list`).then(res => preHandleRes(res));
};

export const addLeave = param =>{
    return axios.post(`${withPrefix}/leave/add`,param).then(res=>preHandleRes(res));
}

export const getMenus = param =>{
    return axios.get(`${withPrefix}/org/getMenus/`+param).then(res=>preHandleRes(res));
}

export const getDepts  = function(){
    return axios.get(`${withPrefix}/org/getDepts`).then(res=>preHandleRes(res));
}

export const login = param =>{
    return axios.post(`${withPrefix}/json/login/`,param).then(res=>preHandleRes(res));
}

export const whoAmI = param =>{
    return axios.post(`${withPrefix}/whoAmI`,param).then(res=>preHandleRes(res));
}

